package websocket;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

public class HttpResponse
{
    private final static Logger LOGGER = Logger.getLogger(HttpResponse.class
        .getName());
    
    public enum HttpVersion { v1_0, v1_1 };
    private HttpVersion httpVersion;
    
    private StatusCode statusCode;

    public enum StatusCode
    {
        CONTINUE_100(100), 
        SWITCHING_PROTOCOLS_101(101),
        OK_200(200),
        MOVED_PERMANENTLY_301(301),
        TEMPORARY_REDIRECT_307(307),
        BAD_REQUEST_400(400),
        UNAUTHORIZED_401(401),
        NOT_FOUND_404(404),
        INTERNAL_SERVER_ERROR_500(500),
        NOT_IMPLEMENTED_501(501),
        SERVICE_UNAVAILABLE_503(503),
        HTTP_VERSION_NOT_SUPPORTED_505(505);
        
        int code;
        String message;
        
        private StatusCode(int code)
        {
            this.code = code;
            
            switch(code)
            {
                case 100:
                    this.message = "Continue";
                    break;
                case 101:
                    this.message = "Switching Protocols";
                    break;
                case 200:
                    this.message = "OK";
                    break;
                case 301:
                    this.message = "Moved Permanently";
                    break;
                case 307:
                    this.message = "Temporary Redirect";
                    break;
                case 400:
                    this.message = "Bad Request";
                    break;
                case 401:
                    this.message = "Unauthorized";
                    break;
                case 404:
                    this.message = "Not Found";
                    break;
                case 500:
                    this.message = "Internal Server Error";
                    break;
                case 501:
                    this.message = "Not Implemented";
                    break;
                case 503:
                    this.message = "Service Unavailable";
                    break;
                case 505:
                    this.message = "HTTP Version not supported";
                    break;
                default:
                    this.message = "Undefined";
            }
        }
    }
    
    private Map<String, String> headers = new HashMap<>();
    private StringBuilder content = new StringBuilder();
    
    public HttpResponse(StatusCode code)
    {
        this.statusCode = code;
        this.httpVersion = HttpVersion.v1_1;
    }
    
    public void appendContent(String content)
    {
        this.content.append(content);
    }
    
    public void setHeader(String key, String value)
    {
        headers.put(key, value);
    }
    
    public String getHeader(String key)
    {
        return headers.get(key);
    }
    
    public String getResponse()
    {
        StringBuilder response = new StringBuilder();
        
        // Status line
        if (httpVersion == HttpVersion.v1_0)
        {
            response.append("HTTP/1.0 ");
        }
        else
        {
            response.append("HTTP/1.1 ");
        }
        
        response.append(statusCode.code);
        response.append(" ");
        response.append(statusCode.message);
        response.append("\r\n");
        //response.append((char) 13); // CR
        //response.append((char) 10); // LF
        
        // Headers
        for (Map.Entry<String, String> entry : headers.entrySet())
        {
            response.append(entry.getKey());
            response.append(": ");
            response.append(entry.getValue());
            response.append("\r\n");
            //response.append((char) 13); // CR
            //response.append((char) 10); // LF
        }
        
        // Empty line after headers
        response.append("\r\n");
        //response.append((char) 13); // CR
        //response.append((char) 10); // LF
        
        // Content
        //response.append("\r\n");
        //response.append(content);
        
        return response.toString();
    }
        
}
